<template>
  <div class="m-l-50 m-t-30 w-500">
    <el-form ref="form" :model="form" :rules="rules" label-width="130px">
      <el-form-item label="分类标题" prop="title">
        <el-input v-model.trim="form.title" class="h-40 w-200" :maxlength=12></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save('form')" :loading="isLoading">提交</el-button>
        <el-button @click="goback()">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<style>
  .form-checkbox:first-child{
    margin-left: 15px;
  }
</style>
<script>
  import http from '../../../assets/js/http'
  import fomrMixin from '../../../assets/js/form_com'
  import _ from 'lodash'
  import _g from '@/assets/js/global'
  import { Button, Dialog, Form, FormItem, Input, Select, Option, CheckboxGroup, Checkbox } from 'element-ui'
  Vue.use(Button)
  Vue.use(Form)
  Vue.use(FormItem)
  Vue.use(Input)
  Vue.use(Select)
  Vue.use(Option)
  Vue.use(CheckboxGroup)
  Vue.use(Checkbox)

  export default {
    data() {
      return {
        isLoading: false,
        id: null,
        form: {
          title: ''
        },
        rules: {
          title: [
            { required: true, message: '请输入分类标题' }
          ]
        }
      }
    },
    methods: {
      save(form) {
        if (this.$route.params.id) {
          this.edit(form)
        } else {
          this.add(form)
        }
      },
      add(form) {
        this.$refs.form.validate((pass) => {
          if (pass) {
            this.isLoading = true
            this.apiPost('admin/category', this.form).then((res) => {
              this.handelResponse(res, (data) => {
                this.isLoading = false
                _g.toastMsg('success', '添加成功')
                setTimeout(() => {
                  this.goback()
                }, 1500)
              }, () => {
                this.isLoading = false
              })
            })
          }
        })
      },
      edit() {
        this.$refs.form.validate((pass) => {
          if (pass) {
            this.isLoading = true
            if (this.password) {
              this.form.password = this.password
            }
            this.apiPut('admin/category/', this.id, this.form).then((res) => {
              this.handelResponse(res, (data) => {
                this.isLoading = false
                _g.toastMsg('success', '编辑成功')
                setTimeout(() => {
                  this.goback()
                }, 1500)
              }, () => {
                this.isLoading = false
              })
            })
          }
        })
      },
      async getCompleteData() {
        this.apiGet('admin/category/' + this.id).then((res) => {
          this.handelResponse(res, (data) => {
            this.form.title = data.title
          })
        })
      }
    },
    created() {
      if (this.$route.params.id) {
        this.id = this.$route.params.id
        this.getCompleteData()
      }
    },
    mixins: [http, fomrMixin]
  }
</script>
